<?php
class CategoryModel extends Model {
	var $tableName = 'cms_category';

	/**
	 * 获取栏目列表
	 * M2
	 */
	public function getCateList($sid) {
		if(($res = S('catelist'.$sid)) === false) {
			$data['siteid'] = $sid;
			$catelist = $this->where($data)->order('display_order asc,id asc')->select();
			$arr = array ();
			foreach ( $catelist as $key => $value ) {
				$arr [] = array (
						'id' => $value['id'],
						'pId' => $value ['pid'],
						'name' => $value ['cname']." (共".$value['content_num']."篇)",
						'open' => (intval($value ['pid']) === 0)?true:false,
						'checked' => true
				);
			}
			S('catelist'.$sid,$arr);
			return json_encode($arr);
		} else {
			return json_encode ($res);
		}
	}

	/**
	 * 获取全站栏目列表
	 * next
	 */
	public function getCateAllList($sid,$sname) {
		$arr = array ();
		$sidarr = explode(",",$sid);
		$snamearr = explode(",",$sname);
		foreach ( $sidarr as $kk => $vsid ) {
			$data['siteid'] = $vsid;
			if(($catelist = S('catelist'.$vsid)) === false) {
				$catelist = $this->where($data)->order('display_order asc,id asc')->select();
				S('catelist'.$vsid,$catelist);
			}
			foreach ( $catelist as $key => $value ) {
				if($value['id']==$value ['siteid']){
					$arr [] = array (
							'id' => $value['id'],
							'pId' => $value ['pid'],
							'siteid' => $value ['siteid'],
							'name' => $snamearr[$kk]." (共".$value['content_num']."篇)",
							'open' => (intval($value ['pid']) === 0)?true:false,
							'checked' => true
					);
				}else{
					$arr [] = array (
							'id' => $value['id'],
							'pId' => $value ['pid'],
							'siteid' => $value ['siteid'],
							'name' => $value ['cname']." (共".$value['content_num']."篇)",
							'open' => (intval($value ['pid']) === 0)?true:false,
							'checked' => true
					);
				}
			}
		}
		return json_encode($arr);
	}

	public function getCateTreeList($sid, $sname) {
		$data['siteid'] = array('in', $sid);
		$catelist = $this->where($data)->field('id, siteid as sid, pid, cname, content_num as count')->order('display_order asc,id asc')->select();
		$arr = tree($catelist, 0, $sname);
		return json_encode($arr);
	}

	/**
	 * 获取全站栏目列表
	 * yan
	 */
	public function getAllCates($sid,$sname) {
		$arr = array ();
		$sidarr = explode(",",$sid);
		$snamearr = explode(",",$sname);
		foreach ( $sidarr as $kk => $vsid ) {
			$data['siteid'] = $vsid;
			if(($catelist = S('catelist'.$vsid)) === false) {
				$catelist = $this->where($data)->order('display_order asc,id asc')->select();
				S('catelist'.$vsid,$catelist);
			}
			foreach ( $catelist as $key => $value ) {
				if($value['id']==$value ['siteid']){
					$arr [] = array (
							'id' => $value['id'],
							'pId' => $value ['pid'],
							'siteid' => $value ['siteid'],
							'name' => $snamearr[$kk],
					);
				}else{
					$arr [] = array (
							'id' => $value['id'],
							'pId' => $value ['pid'],
							'siteid' => $value ['siteid'],
							'name' => $value ['cname'],
					);
				}
			}
		}
		return json_encode($arr);
	}

	/**
	 * 获取栏目列表
	 * next
	 */
	public function channelList($where=array()) {
		$list = $this->field("id,cname,pid,plistid,concat(plistid,'-',id) as bpath,content_num")->where($where)->order('bpath')->select();
		foreach ($list as $key=>$value) {
			$list[$key]['count'] = count(explode('-',$value['bpath']));
		}
		return $list;
	}

	/**
	 * 获取全站栏目列表
	 * next
	 */
	public function channelAllList($sid,$sname) {
		$sidarr = explode(",",$sid);
		$snamearr = explode(",",$sname);
		$data['siteid'] = array('in',$sid);
		$list = $this->field("id,cname,pid,plistid,concat(plistid,'-',id) as bpath,content_num,siteid")->where($data)->order('bpath')->select();
		foreach ($list as $key=>$value) {
			$list[$key]['count'] = count(explode('-',$value['bpath']));
			if($value['id']==$value['siteid']){
				$newkk=0;
				foreach ( $sidarr as $kk => $vsid ) {
					if($vsid==$value['siteid']){
						$newkk=$kk;
						break;
					}
				}
				$list[$key]['cname'] =$snamearr[$newkk];
			}
		}
		return $list;
	}

	/**
	 * 获取子栏目列表
	 * next
	 */
    public function getChildCate($cateid) {
        $sql = 'select id from yl_cms_category where  find_in_set('.intval($cateid).', plistid)';
        $gcate = M()->query($sql);
        $str = '';
        if(!empty($gcate)){
            $str = '$';
        }
        foreach ($gcate as $v){
            $str .= ','.$v['id'];
        }
        return $str;
    }

    /**
     * 获取栏目信息
     * next
     */
    public function getCateDataName($sid,$channelname,$domain,$catenaming) {
    	if (($arrcategory = S('cache_channelname_'.$sid.'_'.$channelname)) === false) {
    		$arrcategory = $this-> field('(case when url <> "" then url when createurl <> "" then createurl
	                            else (concat("'.$domain.'","/",replace(replace((case when catenaming <> ""
	                		 then catenaming else ("'.$catenaming.'") end),"{cateid}",id),"{@ChannelID}",id))) end) as url,
	                            replace(imageurl,"@","'.$domain.'") as imageurl,id,siteid,cname,content,keywords,
	                            description,content_num,pid,catetemplate,arttemplate,catenaming,artnaming,
                				inputtime,updatetime')
    		                            ->where('siteid = '.$sid.' and cname="'.$channelname.'"')
    		                            ->find();
    		S('cache_channelname_'.$sid.'_'.$channelname, $arrcategory);
    	}
    	return $arrcategory;
    }
}

?>
